//
//  CommitDetailsViewController.m
//  Findif
//
//  Created by Mac on 2018/12/14.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "CommitDetailsViewController.h"
#import "MomentCell.h"
#import "Moment.h"
#import "Comment.h"

@interface CommitDetailsViewController ()<UITableViewDelegate,UITableViewDataSource,MomentCellDelegate>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray<Moment*> *momentList;
@property (nonatomic, weak)UILabel * showLabel;

@end

@implementation CommitDetailsViewController

- (void)setMomentList:(NSArray<Moment *> *)momentList{
    
    _momentList = momentList;
    
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"评论";
    
    [self loadData];
    [self setupUI];
}

- (void)loadData{
    
    [[ZRNetworkTool sharedTool] getGoodsCommentsListResultWithGoodsID:self.ID WithBlock:^(BOOL success, id responseData) {
        
        if (success) {
            
            self.momentList = [NSArray yy_modelArrayWithClass:[Moment class] json:responseData[@"data"]];
        }
    }];
}


- (void)setupUI{
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ZRWhiteColor;
    //    tableView.rowHeight = 120 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.separatorInset = UIEdgeInsetsZero;

    self.tableView = tableView;
    
    [self.tableView registerClass:[MomentCell class] forCellReuseIdentifier:@"CommitListTableViewCell"];
    
    [self.view addSubview:self.tableView];
    
    UILabel * showLabel = [UILabel new];
    
    showLabel.textColor = ZRBlackColor;
    showLabel.font = [UIFont systemFontOfSize:17 * SCREENH6];
    showLabel.text = @"暂无评论";
    showLabel.textAlignment = NSTextAlignmentCenter;
    showLabel.hidden = YES;
    
    self.showLabel = showLabel;
    
    [self.view addSubview:self.showLabel];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.momentList.count == 0) {
        
        self.showLabel.hidden = NO;
    }else{
        
        self.showLabel.hidden = YES;
    }
    return [self.momentList count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MomentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommitListTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.moment = [self.momentList objectAtIndex:indexPath.row];
    cell.type = @"detail";
    cell.delegate = self;
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 使用缓存行高，避免计算多次
    Moment *moment = [self.momentList objectAtIndex:indexPath.row];
    return moment.rowHeight;
}
@end
